package com.meng.mmlive.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.PtrHandler;
import com.meng.baselibrary.fragment.BaseFragment;
import com.meng.mmlive.App;
import com.meng.mmlive.R;
import com.meng.mmlive.entity.Adverts;
import com.meng.mmlive.entity.HomeMenu;
import com.meng.mmlive.mvp.presenter.HomePresenter;
import com.meng.mmlive.mvp.view.HomeView;
import com.meng.mmlive.ui.adapter.HomeRecyclerViewAdapter;
import com.meng.mmlive.ui.widget.PtrClassicHeader;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/13.
 */

public class TabHomeFragment extends BaseFragment implements PtrHandler, HomeView {
    @Inject
    HomePresenter presenter;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refleshLayout)
    PtrClassicFrameLayout refleshLayout;
    private HomeRecyclerViewAdapter recyclerViewAdapter;
    @Override
    protected int getContentViewLayout() {
        return R.layout.fragment_tab_home;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this, contentView);
        initPaddingTob();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        PtrClassicHeader header = (PtrClassicHeader) LayoutInflater.from(getActivity()).inflate(R.layout.v_ptrclass_header,null);
        refleshLayout.setHeaderView(header);
        refleshLayout.addPtrUIHandler(header);
        refleshLayout.setPtrHandler(this);
        refleshLayout.setOffsetToRefresh(200);
        recyclerViewAdapter = new HomeRecyclerViewAdapter(context);
        recyclerView.setAdapter(recyclerViewAdapter);

    }
    @Override
    protected void initData() {
        ((App)getActivity().getApplication()).getHomeComponent().inject(this);
        presenter.attachView(this);

        refleshLayout.autoRefresh();//进入自动刷新
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.detachView();
        ButterKnife.unbind(this);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame,content,header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        presenter.loadData();
    }

    @Override
    public void showInitLoadingView() {

    }

    @Override
    public void hideInitLoadingView() {

    }

    @Override
    public void showAdverts(List<Adverts> advertses) {
        recyclerViewAdapter.setAdvertses(advertses);
    }

    @Override
    public void showMainMenus(List<HomeMenu> mainMenus) {
        recyclerViewAdapter.setDatas(mainMenus);
    }

    @Override
    public void showInitLoadFailed(Throwable throwable) {

    }

    @Override
    public void showRefreshLoadFailed(Throwable throwable) {

    }

    @Override
    public void showRefreshLoading() {

    }

    @Override
    public void showRefreshComplete() {
        refleshLayout.refreshComplete();
    }
}
